"""
    封装一些公共方法的基类
"""
from selenium.webdriver.support.wait import WebDriverWait

from tools.get_driver import DriverUtils


class BasePage:
    # 获取驱动对象
    def __init__(self):
        self.driver = DriverUtils.get_driver()
    # 查找元素方法
    def base_find_element(self,loc):
        try:
            el = WebDriverWait(self.driver,10,2).until(lambda x:x.find_element(*loc))
            print(f"excute find_el {loc} success!")
        except Exception as e:
            print(f"excute find_el {loc} error!")
            raise e
        # 返回查找元素
        return el

    # 输入文本方法
    def base_input_text(self,loc,value):
       try:
           el = self.base_find_element(loc)
           # 清空内容
           el.clear()
           # 输入文本
           el.send_keys(value)
           print(f"excute input_text {loc} success!")
       except Exception as e:
           print(f"excute input_text {loc} error!")
           raise e
    # 点击方法
    def base_click(self,loc):
        try:
           el = self.base_find_element(loc)
           el.click()
           print(f"excute click {loc} success!")
        except Exception as e:
           print(f"excute click {loc} error!")
           raise e
    # 获取文本内容
    def base_get_text(self,loc):
        try:
            el = self.base_find_element(loc)
            value = el.text
            print(f"excute get_text {loc} success!")
            return value
        except Exception as e:
            print(f"excute get_text {loc} error!")
            raise e

    # 切换窗口
    def base_switch_windows(self, n):
        """
        :param n:  切换第几个窗口
        :return:
        """
        try:
            # 获取所有的句柄
            handles = self.driver.window_handles
            # 切换窗口
            self.driver.switch_to.window(handles[n])
        except Exception as e:
            print(f"switch to {n} window error!")

if __name__ == '__main__':
    pass


    # base_page= BasePage()
    # base_page.base_input_text("By.CSS_SELECTOR,input[placeholder='Username']","fhj")